//
//  ZHViewController.m
//  LBFacListView
//
//  Created by luckyBoyRoy@163.com on 07/06/2017.
//  Copyright (c) 2017 luckyBoyRoy@163.com. All rights reserved.
//

#import "ZHViewController.h"
#import <Masonry/Masonry.h>
#import <LBFacListView/LBFacListView.h>
#import "ZHTableViewHeaderFooterView.h"
#import "ZHCollectionReusableView.h"
#import <LBFacListView/LBTableViewCell.h>
#import <LBFacListView/LBFacAllHeader.h>

@interface ZHViewController ()

@property (strong, nonatomic) LBFacListView *facCollectionView;

@property (strong, nonatomic) LBFacListView *facTableView;

@property (nonatomic, strong) NSMutableArray *selectedArray;

@property (nonatomic, strong) NSMutableArray *centerSelectedArray;

@property (nonatomic, strong) NSMutableArray *unSelectedArray;

@end

@implementation ZHViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    LBFacListView *showListView;
    Class sectionViewClass;
    showListView = self.facCollectionView;
    
    if (showListView == self.facTableView) {
        sectionViewClass = [ZHTableViewHeaderFooterView class];
    } else {
        sectionViewClass = [ZHCollectionReusableView class];
    }
    
    [self.view addSubview:showListView];
    [showListView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    self.selectedArray = [NSMutableArray array];
    self.unSelectedArray = [NSMutableArray array];
    self.centerSelectedArray = [NSMutableArray array];
    NSMutableArray *modelArray = [NSMutableArray array];
    for (NSInteger i = 0; i < 5; i++) {
        NSString *str = [NSString stringWithFormat:@"选中数据%@",@(i + 1)];
        [self.selectedArray addObject:str];
    }
    for (NSInteger i = 0; i < 6; i++) {
        NSString *str = [NSString stringWithFormat:@"未选中数据%@",@(i + 1)];
        [self.centerSelectedArray addObject:str];
    }
    for (NSInteger i = 0; i < 34; i++) {
        NSString *str = [NSString stringWithFormat:@"未选中数据%@",@(i + 1)];
        [self.unSelectedArray addObject:str];
    }
    
    [modelArray addObject:self.selectedArray];
    [modelArray addObject:self.centerSelectedArray];
    [modelArray addObject:self.unSelectedArray];
    [showListView setCellModelArray:modelArray];
    [showListView reloadListView];
    
    [showListView registerHeaderViewClass:sectionViewClass HeaderModelBlock:^id(NSInteger section) {
        if (0 == section) {
            return @"已选中";
        } else {
            return @"未选中";
        }
    }HeaderViewClick:^(NSInteger index) {
        
    }];
    
    [showListView customCollectionCellWithSize:^CGSize(NSIndexPath *indexPath) {
        if (0 == indexPath.section) {
            return CGSizeMake(150 + arc4random_uniform(50) , 40 );
        } else {
            return CGSizeZero;
        }
    }];
}


- (LBFacListView *)facCollectionView
{
    if (!_facCollectionView) {
        __weak __typeof(&*self)weakSelf = self;
        _facCollectionView = [[LBFacListView alloc]initCollectionViewWithFlowLayout:[[LBFacCustomCollectionViewFlowLayout alloc]init]
                                                                              frame:self.view.bounds cellName:@"ZHTestCollectionViewCell" maxItemsOneLine:3 click:^(NSIndexPath *indexPath) {
            //cell click
            id model = weakSelf.facCollectionView.cellModelArray[indexPath.section][indexPath.row];
            if (0 == indexPath.section) {
                [weakSelf.selectedArray removeObjectAtIndex:indexPath.row];
                [weakSelf.centerSelectedArray addObject:model];
                [weakSelf.facCollectionView.collectionView moveItemAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:0 inSection:1]];
            } else {
                NSMutableArray *array = weakSelf.facCollectionView.cellModelArray[indexPath.section];
                [array removeObjectAtIndex:indexPath.row];
                [weakSelf.selectedArray addObject:model];
                
                NSInteger row = 0;
                
                if (weakSelf.selectedArray.count) {
                    row = (weakSelf.selectedArray.count - 1);
                }
                
                [weakSelf.facCollectionView.collectionView moveItemAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:row inSection:0]];
                
                
            }
        } otherClick:^(NSIndexPath *indexPath, NSInteger index) {
            //view on cell click
        }];
        _facCollectionView.isOneSection = NO;
    }
    return _facCollectionView;
}

- (LBFacListView *)facTableView
{
    if (!_facTableView) {
        _facTableView = [[LBFacListView alloc]initTableViewWithFrame:CGRectZero cellName:@"ZHTestTableViewCell" cellClick:^(NSIndexPath *indexPath) {
            //cell click
        } otherClick:^(NSIndexPath *indexPath, NSInteger index) {
            //view on cell click
        }];
        _facTableView.isOneSection = NO;
    }
    return _facTableView;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

